/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import model.CrumbsUser;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.hibernate.Session;

/**
 *
 * @author Администратор
 */
public class CrumbsUserDAOImpl implements CrumbsUserDAO{

    @Override
    public void addCrumbsUser(CrumbsUser user) throws SQLException {
        Session session = null;
    try {
      session = HibernateUtil.getSessionFactory().openSession();
      session.beginTransaction();
      session.save(user);
      session.getTransaction().commit();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (session != null && session.isOpen()) {
        session.close();
      }
    }
    }

    @Override
    public void updateCrumbsUser(CrumbsUser user) throws SQLException {
        Session session = null;
    try {
      session = HibernateUtil.getSessionFactory().openSession();
      session.beginTransaction();
      session.update(user);
      session.getTransaction().commit();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (session != null && session.isOpen()) {
        session.close();
      }
    }
    }

    @Override
    public CrumbsUser getCrumbsUserById(int user_id) throws SQLException {
        Session session = null;
        CrumbsUser user = null;
    try {
      session = HibernateUtil.getSessionFactory().openSession();
      user = (CrumbsUser) session.get(CrumbsUser.class, user_id);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (session != null && session.isOpen()) {
        session.close();
      }
    }
    return user;
    }

    @Override
    public Collection getAllCrumbsUser() throws SQLException {
         Session session = null;
    List users = new ArrayList<CrumbsUser>();
    try {
      session = HibernateUtil.getSessionFactory().openSession();
      users = session.createCriteria(CrumbsUser.class).list();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (session != null && session.isOpen()) {
        session.close();
      }
    }
    return users;
    }

    @Override
    public void deleteCrumbsUser(CrumbsUser user) throws SQLException {
    Session session = null;
    try {
      session = HibernateUtil.getSessionFactory().openSession();
      session.beginTransaction();
      session.delete(user);
      session.getTransaction().commit();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (session != null && session.isOpen()) {
        session.close();
      }
    }
    }
    
    
}
